﻿cmake_minimum_required(VERSION 3.5)

project(KissPluganPretreatment LANGUAGES CXX)

if(WIN32)
    set(OpenCV_DIR  "D:/lib/opencv-3.4.9/bulid")
endif()

# Output directory - 指向父工程的bin目录
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG
    ${PROJECT_SOURCE_DIR}/../../bin/Debug/plugins)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_DEBUG
    ${PROJECT_SOURCE_DIR}/../../bin/Debug/plugins)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_DEBUG
    ${PROJECT_SOURCE_DIR}/../../bin/Debug/plugins)

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE
    ${PROJECT_SOURCE_DIR}/../../bin/Release/plugins)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_RELEASE
    ${PROJECT_SOURCE_DIR}/../../bin/Release/plugins)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_RELEASE
    ${PROJECT_SOURCE_DIR}/../../bin/Release/plugins)

# 或者使用更通用的方式，通过父工程的变量
# set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG ${CMAKE_BINARY_DIR}/bin/Debug)
# set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_DEBUG ${CMAKE_BINARY_DIR}/bin/Debug)
# set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_DEBUG ${CMAKE_BINARY_DIR}/bin/Debug)

set(CMAKE_INCLUDE_CURRENT_DIR ON)

set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(Qt5
    REQUIRED COMPONENTS Core Gui Widgets Sql Test Xml Network Concurrent)
find_package(Qt5LinguistTools)
add_definitions(-DQT_MESSAGELOGCONTEXT)

# OpenCV配置
if(WIN32)
    find_package(OpenCV REQUIRED)
    include_directories(${OpenCV_INCLUDE_DIRS})
else()
    # macOS下使用pkg-config查找OpenCV
    find_package(PkgConfig REQUIRED)
    pkg_check_modules(OpenCV REQUIRED opencv4)
    include_directories(${OpenCV_INCLUDE_DIRS})
    
    # 确保OpenCV_LIBS被正确设置
    if(NOT OpenCV_LIBS)
        message(WARNING "OpenCV_LIBS is empty, using pkg-config output directly")
        execute_process(
            COMMAND pkg-config --libs opencv4
            OUTPUT_VARIABLE OpenCV_LIBS_RAW
            OUTPUT_STRIP_TRAILING_WHITESPACE
        )
        set(OpenCV_LIBS ${OpenCV_LIBS_RAW})
    endif()
    
    message(STATUS "OpenCV_INCLUDE_DIRS: ${OpenCV_INCLUDE_DIRS}")
    message(STATUS "OpenCV_LIBS: ${OpenCV_LIBS}")
endif()

add_library(KissPluganPretreatment SHARED
    pretreatmentfiltersplugin.cpp
    pretreatmentfiltersplugin.h
    )

# 设置插件的输出名称，确保与主程序期望的名称一致
set_target_properties(KissPluganPretreatment PROPERTIES
    OUTPUT_NAME "KissPluganPretreatment"
    PREFIX "lib"
    SUFFIX ".dylib"
)

target_link_libraries(KissPluganPretreatment
    Qt5::Core
    Qt5::Gui
    Qt5::Widgets
    Qt5::Sql
    Qt5::Xml
    Qt5::Network
    Qt5::Concurrent
    ${OpenCV_LIBS}
    )

# 设置插件的rpath，确保能找到依赖库
set_target_properties(KissPluganPretreatment PROPERTIES
    INSTALL_RPATH "${CMAKE_BINARY_DIR}/bin/Debug"
    BUILD_WITH_INSTALL_RPATH TRUE
    SKIP_BUILD_RPATH FALSE
)
